﻿using System.Collections.Generic;
using System.Data.Common;
using System.Data;

namespace DataAccess
{
    public abstract class DataAccessAbstract<T> : IDataAccess<T>
    {
        /// <summary>
        /// Connection string
        /// </summary>
        protected string connStr;

        /// <summary>
        /// Transaction object
        /// </summary>
        protected DbTransaction trans;

        /// <summary>
        /// Entity converter
        /// </summary>
        protected IEntityConverter<T> converter;

        /// <summary>
        /// Weather open tansaction to execute.
        /// </summary>
        public bool Transaction { get; set; }

        public DataAccessAbstract(string connStr, IEntityConverter<T> converter)
        {
            this.connStr = connStr;
            this.converter = converter;
        }

        #region Execute Function Abstract

        /// <see cref="IDataAccess.QueryEntitys"/>
        public abstract List<T> QueryEntitys(string procStr, DbParam[] dbParams);

        /// <see cref="IDataAccess.FindEntity"/>
        public abstract T FindEntity(string procStr, DbParam[] dbParams);

        /// <see cref="IDataAccess.ExecProc"/>
        public abstract void ExecProc(string procStr, DbParam[] dbParams);

        #endregion

        #region Transaction Function

        /// <summary>
        /// Starts transaction.
        /// </summary>
        /// <param name="conn">db connection</param>
        protected virtual void StartTrans(DbConnection conn)
        {
            if (Transaction)
            {
                trans = conn.BeginTransaction();
            }
        }

        /// <summary>
        /// Commits transaction.
        /// </summary>
        /// <param name="conn">db connection</param>
        protected virtual void CommitTrans()
        {
            if (Transaction && trans != null)
            {
                trans.Commit();
            }
        }

        /// <summary>
        /// Rollbacks transaction.
        /// </summary>
        /// <param name="conn">db connection</param>
        protected virtual void RollbackTrans()
        {
            if (Transaction && trans != null)
            {
                trans.Rollback();
            }
        }

        /// <summary>
        /// Disposes transaction.
        /// </summary>
        protected virtual void CloseTrans()
        {
            if (Transaction && trans != null)
            {
                trans.Dispose();
            }
        }

        #endregion

        #region Value Function

        /// <summary>
        /// Converts DataAccess.DbParam to DbParameter
        /// </summary>
        /// <param name="dbParams">db param objects</param>
        /// <returns>db parameters</returns>
        protected abstract DbParameter[] ConvertParams(DbParam[] dbParams);

        /// <summary>
        /// Sets the DbParameter value to DbParam
        /// </summary>
        /// <param name="dbParams">db param objects</param>
        protected virtual void SetOutputValue(DbParam[] dbParams)
        {
            foreach (DbParam p in dbParams)
            {
                switch (p.Direction)
                {
                    case ParameterDirection.InputOutput:
                    case ParameterDirection.Output:
                    case ParameterDirection.ReturnValue:
                        p.Value = p.DbParameter.Value;
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion
    }
}
